<?php
namespace Service;

use Illuminate\Database\Capsule\Manager as DB;
use Library\Log;
use Library\Redis;
use Model\Cnf;


/**
 * CnfService
 * @author los_gsy
 */
class CnfService extends BaseService {
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
	}


	/**
	 * test
	 */
	static public function test() {
		return __METHOD__;
	}


	/**
	 * 添加单个配置项
	 * @param array $data
	 * @return int
	 */
	static public function addCnf($data) {
		//定义变量
		$now = time();

		//将配置项插入数据库
		$data['create_at'] = $now;
		$data['update_at'] = $now;
		$id = Cnf::add($data);

		//插入成功，则删除Cache
		if ($id) {
			$type = $data['type'];
			Redis::del("cnf:$type");
		}

		return $id;
	}


	/**
	 * 批量添加配置
	 * @param array $cnf_list
	 * @return mixed
	 */
	static public function batchAddCnf($cnf_list) {
		//定义变量
		$now = time();
		$type_list = [];

		//批量添加
		DB::beginTransaction();
		foreach ($cnf_list as $i => $cnf) {	//循环遍历添加
			if ($cnf['type'] && $cnf['k']) {
				$cnf['create_at'] = $now;
				$cnf['update_at'] = $now;
				$id = Cnf::add($cnf);
				if (!$id) {
					DB::rollBack();
					return $i;	//返回插入失败的cnf，type和k组合键重复
				}
				//归集type列表
				if (!in_array($cnf['type'], $type_list)) {
					$type_list[] = $cnf['type'];
				}
			}
		}
		DB::commit();

		//批量添加成功，删除Cache
		$redis_key_list = [];
		foreach ($type_list as $type) {
			$redis_key_list[] = "cnf:$type";
		}
		Redis::del($redis_key_list);

		return true;
	}


	/**
	 * 删除配置项
	 * @param int $id
	 * @return mixed
	 */
	static public function delCnf($id) {
		//删除Cache
		$cnf = Cnf::get($id);
		if (!$cnf) {
			return false;
		}
		$redis_key = 'cnf:' . $cnf->type;
		Redis::del($redis_key);

		//删除配置项
		$result = Cnf::del($id);

		//Success
		return $result;
	}


	/**
	 * 批量删除配置
	 * @param array $id_list
	 * @return int
	 */
	static public function batchDelCnf($id_list) {
		//删除Cache
		$cnf_list = Cnf::getList($id_list);
		$type_list = [];
		foreach ($cnf_list as $cnf) {
			if (!in_array($cnf['type'], $type_list)) {
				$type_list[] = $cnf['type'];
			}
		}
		$redis_key_list = [];
		foreach ($type_list as $type) {
			$redis_key_list[] = "cnf:$type";
		}
		Redis::del($redis_key_list);

		//批量删除配置
		$result = Cnf::delList($id_list);

		return $result;
	}


	/**
	 * 修改配置项
	 * @param array $data
	 * @param int $id
	 * @return int
	 */
	static public function editCnf($id, $data) {
		//检验参数
		$data['update_at'] = time();

		//保存配置项
		$result = Cnf::update($id, $data);

		//保存配置项成功，则删除Cache
		if ($result) {
			$redis_key = 'cnf:' . $data['type'];
			Redis::del($redis_key);
		}

		return $result;
	}


	/**
	 * 批量修改配置
	 * @param array $cnf_list
	 * @return mixed
	 */
	static public function batchEditCnf($cnf_list) {
		//定义变量
		$now = time();
		$type_list = [];

		//批量修改配置
		DB::beginTransaction();
		foreach ($cnf_list as $cnf) {
			$cnf['create_at'] = $now;
			$cnf['update_at'] = $now;
			$result = Cnf::update($cnf['id'], $cnf);
			if (!$result) {
				DB::rollback();
				return $cnf['id'];	//返回更新失败的cnf，type和k组合键重复
			}

			//归集type列表
			if (!in_array($cnf['type'], $type_list)) {
				$type_list[] = $cnf['type'];
			}
		}
		DB::commit();

		//批量修改成功，删除Cache
		$redis_key_list = [];
		foreach ($type_list as $type) {
			$redis_key_list[] = "cnf:$type";
		}
		Redis::del($redis_key_list);

		return true;
	}


	/**
	 * 搜索配置
	 * @param array $param
	 * @param int $page
	 * @param int $pagesize
	 * @return array
	 */
	static public function searchCnf($param, $page, $pagesize) {
		//搜索配置
		$cnfResult = [];
		$cnfResult['cnt'] = DB::table('cnf')
			->where('type', 'like', "%{$param['type']}%")
			->where('k', 'like', "%{$param['k']}%")
			->where('v', 'like', "%{$param['v']}%")
			->orderBy('update_at', 'desc')
			->orderBy('type', 'asc')
			->orderBy('k', 'asc')
			->count();
		$cnfResult['list'] = DB::table('cnf')
			->where('type', 'like', "%{$param['type']}%")
			->where('k', 'like', "%{$param['k']}%")
			->where('v', 'like', "%{$param['v']}%")
			->orderBy('update_at', 'desc')
			->orderBy('type', 'asc')
			->orderBy('k', 'asc')
			->skip(($page - 1) * $pagesize)
			->take($pagesize)
			->get();
		return $cnfResult;
	}


}